﻿#region License

// PentaBiz - Sustainable Software Development Framework Copyright (C) 2013 Zoltán Csizmazia
// 
// This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 

#endregion

using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.IO;
using System.Linq;
using PentaBiz.Cloud.CloudDb;
using PentaBiz.Cloud.CloudDb.EntityModel.MetaModel;
using PentaBiz.Cloud.Services.DynamicService.CodeGeneration.Templating;

namespace PentaBiz.Cloud.Services.DynamicService.CodeGeneration
{
    public static class CodeGenService
    {
        [Pure]
        public static string[] GetCustomerAssemblyInfoTemplate()
        {
            return EmbeddedResourceReader.ReadAllResourceLines(o => o.EndsWith("CustomerAssemblyInfo.cs"));
        }

        [Pure]
        public static string[] GetCustomerDbAndServiceModelTemplate()
        {
            return EmbeddedResourceReader.ReadAllResourceLines(o => o.EndsWith("CustomerDbAndServiceModel.cs"));
        }

        [Pure]
        public static string[] GetCustomerDbViewsTemplate()
        {
            return EmbeddedResourceReader.ReadAllResourceLines(o => o.EndsWith("CustomerDbViews.cs"));
        }

        [Pure]
        public static string GenerateCustomerAssemblyInfo(this MetaChangeSet metaChangeSet)
        {
            //read template lines
            string[] lines = GetCustomerAssemblyInfoTemplate();

            //create data context
            var dc = new AssemblyInfoDataContext
                {
                    ChangeSetId = metaChangeSet.Id.ToString(),
                    ApplicationName = metaChangeSet.MetaApplication.Name,
                    ApplicationVersion = metaChangeSet.MetaVersion,
                    ApplicationGuid = metaChangeSet.MetaApplication.Id.ToString(),
                };

            //render with the template with the AssemblyInfoDataContext
            return TemplateEngine.Render(lines, dc);
        }

        [Pure]
        public static string GenerateCustomerDbAndServiceModel(this MetaChangeSet metaChangeSet)
        {
            List<string> lines = GetCustomerDbAndServiceModelTemplate().ToList();

            string code = TemplateEngine.Render(lines.ToArray(), metaChangeSet.MetaApplication);

            File.WriteAllText("PentaTrade." + metaChangeSet.Id.ToString("N") + ".cs", code);

            return code;
        }

        [Pure]
        public static string GenerateCustomerDbViews(this PreGeneratedViews preGeneratedViews)
        {
            var lines = GetCustomerDbViewsTemplate();

            return TemplateEngine.Render(lines, preGeneratedViews);
        }

        #region Nested type: AssemblyInfoDataContext

        private class AssemblyInfoDataContext
        {
            public const string Guid = "FF7C634B-56FE-4AD3-8E8B-539F5253AEDF";
            public string ApplicationVersion { get; set; }
            public string ApplicationName { get; set; }
            public string ApplicationGuid { get; set; }
            public string ChangeSetId { get; set; }
        }

        #endregion

        //public static string GenerateViews(this Db)
        //public static string Generate
    }
}